﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Exercitation.Model;
using Yipusoft.Exercitation.BLL;

public partial class Combat_SoldierScoreSync : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            InitData();
    }
    private void InitData()
    {
        for (int i = 2010; i <= 2020; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }

        for (int i = 1; i <= 12; i++)
        {
            ddlMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }

        ddlYear.SelectedValue = DateTime.Now.Year.ToString();
        ddlMonth.SelectedValue = DateTime.Now.Month.ToString();

    }

    public string GetTaskID()
    {
        TaskBaseInfo taskInfo = TaskBase.GetList(" Year(EndDate)=" + ddlYear.SelectedValue + " And Month(EndDate)=" + ddlMonth.SelectedValue + " And IsOn='1'").FirstOrDefault();
        return taskInfo != null ? taskInfo.TaskID.ToString() : Guid.Empty.ToString();
    }
    //获取士兵训练成绩及格率
    public void GetAcceptability(Yipusoft.SoldierArchives.Model.SoldierArchiveInfo soldierInfo)
    {
        if (soldierInfo == null)
            return;
        //if (string.IsNullOrEmpty(soldierID))
        //    return 0;
        TaskBaseInfo taskInfo = Yipusoft.Exercitation.BLL.TaskBase.GetTaskBase(new Guid( GetTaskID()));
        if (taskInfo == null)
            return;


        SoldierRankInfo rankInfo = SoldierRank.GetSoldierRank(soldierInfo.SoldierID.ToString());
        if (rankInfo == null)
            return;
        //此警衔此岗位应训练的项目
        List<TaskSubjectInfo> taskSubjectList = TaskSubject.GetList(" TaskItemID IN( Select ID From TaskItems Where RankID='" + rankInfo.RankID + "' And StationID In(Select StationID From SoldierStation Where SoldierID='" + soldierInfo.SoldierID + "'))And TaskID='" + taskInfo.TaskID + "'");
        if (taskSubjectList == null || taskSubjectList.Count() <= 0)
            return;

        int count = 0;
        List<TaskScoreInfo> taskScoreList = TaskScore.GetList(" SoldierID='" + soldierInfo.SoldierID + "' And TaskID='" + taskInfo.TaskID + "'");
        if (taskScoreList == null || taskScoreList.Count() <= 0)
            return;

        int temp;
        //try
        //{
        foreach (TaskSubjectInfo info in taskSubjectList)
        {
            TaskScoreInfo scoreInfo = taskScoreList.Where(o => o.SkillID.Equals(info.SkillID)).FirstOrDefault();
            if (scoreInfo != null)
            {
                TaskSkillInfo skillInfo = TaskSkill.GetTaskSkill(scoreInfo.SkillID);
                if (skillInfo != null)
                {

                    switch (skillInfo.SkillItemID)
                    {
                        case "1"://合格
                            if (scoreInfo.Score.Equals(skillInfo.SkillStandard))
                                count += 1;
                            break;
                        case "3"://数量                                
                            if (int.TryParse(scoreInfo.Score.ToString(), out temp))
                            {
                                if (int.Parse(scoreInfo.Score) >= int.Parse(skillInfo.SkillStandard))
                                {
                                    count += 1;
                                }
                            }

                            break;
                        case "2":
                            DateTime scoredt = DateTime.Parse(scoreInfo.Score);
                            DateTime standard = DateTime.Parse(skillInfo.SkillStandard);
                            if (scoredt <= standard)
                                count += 1;
                            break;
                    }
                }
            }

        }


        //}
        //catch (Exception ex)
        //{

        //    return 0;
        //}

        TaskSoldierScoreInfo info2 = TaskSoldierScore.GetList(" TaskID='" + taskInfo.TaskID + "' And SoldierID='" + soldierInfo.SoldierID + "'").FirstOrDefault();
        if (info2 != null)
        {
            TaskSoldierScore.Delete(info2.ID);
        }
        TaskSoldierScoreInfo sinfo = new TaskSoldierScoreInfo();
        sinfo.ID = Guid.NewGuid();
        sinfo.SoldierID = new Guid(soldierInfo.SoldierID);
        sinfo.DepartmentID = soldierInfo.DepartmentID;
        sinfo.CardID = soldierInfo.CardID;
        sinfo.SetDate = DateTime.Now;
        sinfo.Score = decimal.Parse(count.ToString()) / decimal.Parse(taskSubjectList.Count.ToString());
        sinfo.TaskDate = taskInfo.EndDate;
        sinfo.TaskID = taskInfo.TaskID;
        TaskSoldierScore.Insert(sinfo);

        //return

        //List<TaskScoreInfo> scoreList = TaskScore.GetList(" TaskID='"+taskInfo.TaskID+"' And SoldierID='"+objID.ToString()+"'");
    }


    protected void btnSync_Click(object sender, EventArgs e)
    {
        Yipusoft.SoldierArchives.BLL.Soldier soldier = new Yipusoft.SoldierArchives.BLL.Soldier();
        List<Yipusoft.SoldierArchives.Model.SoldierArchiveInfo> soldierList= soldier.GetSoldiersByWhere(" IsDelete=0 And Left(DepartmentID,4)='1301'", "SoldierID", "Desc", 1000, 0);
        foreach (Yipusoft.SoldierArchives.Model.SoldierArchiveInfo info in soldierList)
        {
            GetAcceptability(info);
        }
    }
}
